package api

import (
	"github.com/gin-gonic/gin"
	"github.com/mssola/user_agent"
	"github.com/oschwald/geoip2-golang"
	"go.uber.org/zap"
	"jms/global"
	"jms/models"
	"net"
	"strings"
	"time"
)

func GetLvList(ctx *gin.Context) {
	var shop []models.Shop
	tx := global.Db.Where("id > ?", 0).Preload("Tags").Order("sort desc").Find(&shop)
	for k, v := range shop {
		if v.BannerImage != "" {
			shop[k].BannerImage = global.ServerConfig.Oss.Url + v.BannerImage
		}
	}
	if tx.Error != nil {
		zap.S().Info(tx.Error, tx.RowsAffected)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": shop,
	})
}

// GetLvInfo 获取详情
func GetLvInfo(ctx *gin.Context) {
	param := models.Shop{}
	if err := ctx.ShouldBindJSON(&param); err != nil {
		zap.S().Info("绑定参数失败", err.Error())
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var shop models.Shop
	tx := global.Db.Where("id  = ?", param.Id).Preload("Hotel").Preload("Tags").Preload("Costs").Preload("Notice").Order("sort desc").Find(&shop)
	if tx.Error != nil {
		zap.S().Info(tx.Error, tx.RowsAffected)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	if shop.Hotel != nil {
		for k, v := range shop.Hotel {
			splitArr := strings.Split(v.Images, ",")
			str := ""
			for _, splitV := range splitArr {
				str = str + global.ServerConfig.Oss.Url + splitV + ","
			}
			shop.Hotel[k].Images = str[:len(str)-1]
		}
	}

	if shop.BannerImage != "" {
		shop.BannerImage = global.ServerConfig.Oss.Url + shop.BannerImage
	}

	group_images := map[int64]interface{}{}
	if shop.GroupImages != "" {
		//分割字符串
		split := strings.Split(shop.GroupImages, ",")
		for k, v := range split {
			group_images[int64(k)] = global.ServerConfig.Oss.Url + v
		}
	}

	strokeImages := map[int64]interface{}{}
	if shop.StrokeImages != "" {
		//分割字符串
		split := strings.Split(shop.StrokeImages, ",")
		for k, v := range split {
			strokeImages[int64(k)] = global.ServerConfig.Oss.Url + v
		}
	}
	ctx.JSON(200, gin.H{
		"code":          200,
		"data":          shop,
		"group_images":  group_images,
		"stroke_images": strokeImages,
	})
}

func DayInfo(ctx *gin.Context) {
	param := models.DayInfo{}
	if err := ctx.ShouldBindJSON(&param); err != nil {
		zap.S().Info("绑定参数失败", err.Error())
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	//查询数据
	var dayInfo []models.DayInfo
	tx := global.Db.Where("shop_id = ?", param.ShopId).Order("`order` asc").Find(&dayInfo)
	if tx.Error != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}

	for k, v := range dayInfo {
		if v.Image != "" {
			dayInfo[k].Image = global.ServerConfig.Oss.Url + v.Image
		}
	}

	ctx.JSON(200, gin.H{
		"code": 200,
		"data": dayInfo,
	})
}

func Open(ctx *gin.Context) {
	var err error
	// 打开 GeoIP2 数据库文件
	geoDB, err := geoip2.Open("./GeoLite2-City.mmdb")
	if err != nil {
		zap.S().Fatal("无法打开 GeoIP2 数据库文件", err)
	}

	param := models.ChannelRecord{}
	if err := ctx.ShouldBindJSON(&param); err != nil {
		zap.S().Info("绑定参数失败", err.Error())
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	//获取ip
	param.IP = ctx.ClientIP()
	//获取ua
	param.UA = ctx.Request.UserAgent()
	param.CreatedTime = time.Now()
	ip := net.ParseIP(param.IP)
	if ip == nil {
		zap.S().Info("无效的IP地址", param.IP)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "无效的IP地址",
		})
		return
	}
	record, err := geoDB.City(ip)
	if err != nil {
		zap.S().Info("查询城市信息失败", err)
		//ctx.JSON(200, gin.H{
		//	"code": 400,
		//	"msg":  "查询城市信息失败",
		//})
		//return
	}

	param.Address = record.City.Names["zh-CN"] // 获取中文城市名
	if param.Address == "" {
		param.Address = record.City.Names["en"] // 如果没有中文城市名，则获取英文城市名
	}
	// 解析ua
	uaParser := user_agent.New(param.UA)
	// 提取机型信息
	param.Model = uaParser.Model()

	var ChannelRecord models.ChannelRecord
	global.Db.Model(&ChannelRecord).Create(&param)

}
